﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace ShootMan
{
    public class GameProcessSpawnEnemy
    {
        public IEnumerator Process(GameRoot root)
        {
            var gameRoot = root;
            var totalWeigth = 0;
            foreach (var iter in ShootMan.cfg_spawn_enemy.Instance)
            {
                totalWeigth += iter.Value.RANDOM_WEIGHT;
            }

            while (true)
            {
                yield return null;

                yield return new WaitForSeconds(1.0f);

                //随机一个敌人
                var randomWeight = shaco.Base.Utility.Random(0, totalWeigth);
                var startWeigth = 0;
                var endWeigth = 0;
                ShootMan.cfg_spawn_enemy.RowData randomConfigData = null;
                foreach (var iter in ShootMan.cfg_spawn_enemy.Instance)
                {
                    endWeigth += iter.Value.RANDOM_WEIGHT;
                    if (randomWeight >= startWeigth && randomWeight <= endWeigth)
                    {
                        randomConfigData = iter.Value;
                        break;
                    }
                    startWeigth += iter.Value.RANDOM_WEIGHT;
                }

                var newEnemyGameObject = shaco.GameHelper.objectpool.InstantiateWithKey(randomConfigData.PREFAB_NAME, () =>
                {
                    return shaco.GameHelper.res.LoadEx<UnityEngine.GameObject>($"{GlobalDefines.HOTUPDATE_ROOT}" + randomConfigData.PREFAB_NAME);
                });

                //重置重力
                var rigidbody = newEnemyGameObject.GetComponent<Rigidbody>();
                if (null != rigidbody)
                {
                    var oldConstraints = rigidbody.constraints;
                    rigidbody.constraints = RigidbodyConstraints.FreezeAll;
                    rigidbody.constraints = oldConstraints;
                }

                //随机一个出生点
                var randomPositionX = shaco.Base.Utility.Random(gameRoot.randomSpawnMinPositionX.transform.position.x, gameRoot.randomSpawnMaxPositionX.transform.position.x);
                newEnemyGameObject.transform.position = new Vector3(randomPositionX, 0, newEnemyGameObject.transform.position.z);
                newEnemyGameObject.transform.SetParent(gameRoot.enemySpawnPoint.transform, false);

                //通知其他玩家产生了一个敌人
                var newEvent = shaco.GameHelper.objectpool.Instantiate(() => new ShootMan.NetEvent.C2S_NotifySpawnEnemy());
                newEvent.playerID = ShootMan.UserData.playerID;
                newEvent.enemyPrefabName = randomConfigData.PREFAB_NAME;
                newEvent.position = newEnemyGameObject.transform.position;
                NetServiceComponent.SendMessage(newEvent);
                newEvent.RecyclingWithPool();
            }
        }

        public void ProcessEnd()
        {
            shaco.Log.Info("GameProcessSpawnEnemy ProcessEnd...", Color.white);
        }
    }
}